package com.ruoyi.web.controller.repair;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.DataUtils;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.project.domain.HouseRepairCheckT;
import com.ruoyi.project.service.IHouseProjectTService;
import com.ruoyi.repairDoor.domain.HouseRepairDoorT;
import com.ruoyi.repairGroup.service.IHouseRepairDoorTService;
import com.ruoyi.setup.domain.HouseSetupT;
import com.ruoyi.setup.service.IHouseSetupTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 户资金修改Controller
 *
 * @author tyq
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/repair/repairDoor")
public class HouseRepairDoorTController extends BaseController {
    @Autowired
    private IHouseRepairDoorTService houseRepairDoorTService;

    @Autowired
    private IHouseProjectTService houseProjectTService;
    @Autowired
    private IHouseDoorTService houseDoorTService;
    @Autowired
    private IHouseSetupTService houseSetupTService;
    private String projectId;

    public HouseRepairDoorTController(IHouseProjectTService houseProjectTService) {
        this.houseProjectTService = houseProjectTService;
    }

    /**
     * 查询维修列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseRepairDoorT houseRepairDoorT) {
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        HouseDoorT doorVO = new HouseDoorT();
        HouseProjectT projectVO = new HouseProjectT();
        projectId = houseRepairDoorT.getProjectId();
        List<HouseDoorT> doorList = new ArrayList<HouseDoorT>();
        if (projectId != null) {
            projectVO = houseProjectTService.selectHouseProjectTByProjectId(projectId);
            if (projectVO == null) {
                return getDataTable(doorList);
            }
            if (projectVO != null && "1".equals(projectVO.getProjectSts())) {
                return getDataTable(doorList);
            }
            if ("4".equals(projectVO.getProjectType())) {
                doorVO.setAreaName(projectVO.getAreaName());
                doorVO.setGroupName(houseRepairDoorT.getGroupName());
                doorVO.setUnitName(houseRepairDoorT.getUnitName());
                doorVO.setDoorName(houseRepairDoorT.getDoorName());
                doorList = houseDoorTService.selectHouseDoorTList(doorVO);
            }
        }
        return getDataTable(doorList);
    }

    @GetMapping(value = "/rightList")
    public TableDataInfo rightList(HouseRepairDoorT houseRepairDoorT) {
        HouseProjectT projectVO = new HouseProjectT();
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        List<HouseRepairDoorT> list = new ArrayList<HouseRepairDoorT>();
        projectId = houseRepairDoorT.getProjectId();
        projectVO = houseProjectTService.selectHouseProjectTByProjectId(projectId);
        if (projectVO != null) {
            if (!(projectVO != null && "1".equals(projectVO.getProjectSts()) && "4".equals(projectVO.getProjectType()))) {
                list = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorT);
                if (list.size() != 0) {
                    if (getDataTableSumJudge(list)) {
                        houseRepairDoorVO = houseRepairDoorTService.selectHouseRepairDoorSum(houseRepairDoorT);
                        houseRepairDoorVO.setHouseId("合计");
                        list.add(houseRepairDoorVO);
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出户资金修改列表
     */
    @Log(title = "小区维修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseRepairDoorT houseRepairDoorT) {
        List<HouseRepairDoorT> list = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorT);
        ExcelUtil<HouseRepairDoorT> util = new ExcelUtil<HouseRepairDoorT>(HouseRepairDoorT.class);
        util.exportExcel(response, list, "户资金修改数据");
    }

    /**
     * 获取户资金修改详细信息
     */

    @GetMapping(value = "/{houseId}/{projectId}")
    public AjaxResult getInfo(@PathVariable("houseId") String houseId, @PathVariable("projectId") String projectId) {
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        houseRepairDoorVO.setHouseId(houseId);
        houseRepairDoorVO.setProjectId(projectId);
        return AjaxResult.success(houseRepairDoorTService.getRepairDetail(houseRepairDoorVO));
    }


    @GetMapping(value = "{projectId}")
    public AjaxResult getProjectAreaName(@PathVariable("projectId") String projectId) {
        HouseProjectT projectVO = houseProjectTService.selectHouseProjectTByProjectId(projectId);
        if (projectVO == null) {
            return AjaxResult.error("工程编号不正确,查询条件楼宇搜索失败!");
        }
        return AjaxResult.success(houseRepairDoorTService.getProjectAreaName(projectId));
    }


    /**
     * 新增户资金修改
     */
    @Log(title = "新增", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/insertRepairDoor/{houseIds}/{projectId}")
    public AjaxResult insertRepairDoor(@PathVariable("houseIds") String[] houseIds, @PathVariable("projectId") String projectId) {
        for (String houseId : houseIds) {

            List<HouseRepairDoorT> houseRepairList=houseRepairDoorTService.selectHouseRepairTListByHouseId(houseId);
            if(houseRepairList.size()>0){
                return AjaxResult.error("该户存在未执行的项目!");
            }

            HouseRepairDoorT vo = new HouseRepairDoorT();
            HouseRepairDoorT voTemp = new HouseRepairDoorT();
            vo.setHouseId(houseId);
            vo.setProjectId(projectId);
            voTemp = houseRepairDoorTService.selectRepairQuery(vo);
            if (voTemp != null) {
                return AjaxResult.error("该维修列表下以存在此用户,请在右侧输入工程编号进行搜索");
            }
        }
        return toAjax(houseRepairDoorTService.insertHouseRepairDoorT(houseIds, projectId));
    }

    /**
     * 删除
     */
    @Log(title = "删除", businessType = BusinessType.UPDATE)
    @GetMapping("/delRepairDoor/{houseIds}/{projectId}")
    public AjaxResult deleteRepair(@PathVariable("houseIds") String[] houseIds, @PathVariable("projectId") String projectId) {
        return toAjax(houseRepairDoorTService.deleteHouseRepairDoorTByDoorIds(houseIds, projectId));
    }

    /**
     * 修改户资金修改
     */
    @Log(title = "户资金修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseRepairDoorT houseRepairDoorT) {
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        HouseRepairCheckT houseRepairCheckO = new HouseRepairCheckT();
        HouseProjectT houseProjectT = new HouseProjectT();
        houseRepairDoorVO = houseRepairDoorTService.getRepairDetail(houseRepairDoorT);
        Double retained = getRetained();
        houseProjectT.setProjectId(houseRepairDoorT.getProjectId());
        houseProjectT.setRepairMoney(houseRepairDoorVO.getRepairMoney());
        houseProjectT.setCheckMoney(houseRepairDoorT.getRepairMoney());
        HouseSetupT houseSetupT = houseSetupTService.selectHouseSetupTByFeeStandards();
        if("Y".equals(houseSetupT.getBeInDebtSys())){
            if("Y".equals(houseSetupT.getBuckleRateSys())){
                if (houseRepairDoorT.getRepairMoney() == 0) {
                    houseRepairDoorVO.setNoMoney(0d);
                    houseRepairDoorVO.setPayBalance(0d);
                    houseRepairDoorVO.setPayRate(0d);
                } else if (houseRepairDoorVO.getBalance() <= houseRepairDoorVO.getBudgetMoney() * retained) {
                    if (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained <= 0) {
                        houseRepairDoorVO.setNoMoney(0d);
                        houseRepairDoorVO.setPayBalance(houseRepairDoorT.getRepairMoney());
                        houseRepairDoorVO.setPayRate(0d);
                    } else if (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained >= houseRepairDoorT.getRepairMoney()) {
                        houseRepairDoorVO.setNoMoney(0d);
                        houseRepairDoorVO.setPayBalance(0d);
                        houseRepairDoorVO.setPayRate(houseRepairDoorT.getRepairMoney());
                    } else {
                        houseRepairDoorVO.setNoMoney(0d);
                        houseRepairDoorVO.setPayRate(DataUtils.doubleScale2(houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained));
                        houseRepairDoorVO.setPayBalance(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney() - houseRepairDoorVO.getPayRate()));
                    }
                } else if (houseRepairDoorVO.getBalance() - houseRepairDoorT.getRepairMoney() >= houseRepairDoorVO.getBudgetMoney() * retained) {
                    houseRepairDoorVO.setNoMoney(0d);
                    houseRepairDoorVO.setPayBalance(houseRepairDoorT.getRepairMoney());
                    houseRepairDoorVO.setPayRate(0d);
                } else {
                    if (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained >= houseRepairDoorT.getRepairMoney()) {
                        houseRepairDoorVO.setNoMoney(0d);
                        houseRepairDoorVO.setPayBalance(DataUtils.doubleScale2(houseRepairDoorVO.getBalance()-houseRepairDoorVO.getBudgetMoney() * retained));
                        houseRepairDoorVO.setPayRate(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney()-houseRepairDoorVO.getPayBalance()));
                    } else {
                        houseRepairDoorVO.setNoMoney(0d);
                        houseRepairDoorVO.setPayRate(houseRepairDoorVO.getRateMoney());
                        houseRepairDoorVO.setPayBalance(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney()-houseRepairDoorVO.getPayRate()));
                    }
                }
            }else{
                houseRepairDoorVO.setNoMoney(0d);
                houseRepairDoorVO.setPayBalance(houseRepairDoorT.getRepairMoney());
                houseRepairDoorVO.setPayRate(0d);
            }
        }else{
            if("Y".equals(houseSetupT.getBuckleRateSys())){
                if (houseRepairDoorT.getRepairMoney() == 0) {
                    houseRepairDoorVO.setNoMoney(0d);
                    houseRepairDoorVO.setPayBalance(0d);
                    houseRepairDoorVO.setPayRate(0d);
                } else if (houseRepairDoorVO.getBalance() <= houseRepairDoorVO.getBudgetMoney() * retained) {
                    if (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained <= 0) {
                        houseRepairDoorVO.setNoMoney(houseRepairDoorT.getRepairMoney());
                        houseRepairDoorVO.setPayBalance(0d);
                        houseRepairDoorVO.setPayRate(0d);
                    } else if (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained >= houseRepairDoorT.getRepairMoney()) {
                        houseRepairDoorVO.setNoMoney(0d);
                        houseRepairDoorVO.setPayBalance(0d);
                        houseRepairDoorVO.setPayRate(houseRepairDoorT.getRepairMoney());
                    } else {
                        houseRepairDoorVO.setNoMoney(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney() - (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained)));
                        houseRepairDoorVO.setPayBalance(0d);
                        houseRepairDoorVO.setPayRate(DataUtils.doubleScale2(houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained));
                    }
                } else if (houseRepairDoorVO.getBalance() - houseRepairDoorT.getRepairMoney() >= houseRepairDoorVO.getBudgetMoney() * retained) {
                    houseRepairDoorVO.setNoMoney(0d);
                    houseRepairDoorVO.setPayBalance(houseRepairDoorT.getRepairMoney());
                    houseRepairDoorVO.setPayRate(0d);
                } else {
                    if (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained >= houseRepairDoorT.getRepairMoney()) {
                        houseRepairDoorVO.setNoMoney(0d);
                        houseRepairDoorVO.setPayBalance(DataUtils.doubleScale2(houseRepairDoorVO.getBalance()-houseRepairDoorVO.getBudgetMoney() * retained));
                        houseRepairDoorVO.setPayRate(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney()-houseRepairDoorVO.getPayBalance()));
                    } else {
                        houseRepairDoorVO.setNoMoney(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney() - (houseRepairDoorVO.getBalanceSum() - houseRepairDoorVO.getBudgetMoney() * retained)));
                        houseRepairDoorVO.setPayBalance(DataUtils.doubleScale2(houseRepairDoorVO.getBalance()-houseRepairDoorVO.getBudgetMoney() * retained));
                        houseRepairDoorVO.setPayRate(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney()-houseRepairDoorVO.getPayBalance()));
                    }
                }
            }else{

                if (houseRepairDoorT.getRepairMoney() == 0) {
                    houseRepairDoorVO.setNoMoney(0d);
                    houseRepairDoorVO.setPayBalance(0d);
                    houseRepairDoorVO.setPayRate(0d);
                } else if (houseRepairDoorVO.getBalance() <= houseRepairDoorVO.getBudgetMoney() * retained) {
                    houseRepairDoorVO.setNoMoney(houseRepairDoorT.getRepairMoney());
                    houseRepairDoorVO.setPayBalance(0d);
                    houseRepairDoorVO.setPayRate(0d);
                } else if (houseRepairDoorVO.getBalance() - houseRepairDoorT.getRepairMoney() >= houseRepairDoorVO.getBudgetMoney() * retained) {
                    houseRepairDoorVO.setNoMoney(0d);
                    houseRepairDoorVO.setPayBalance(houseRepairDoorT.getRepairMoney());
                    houseRepairDoorVO.setPayRate(0d);
                } else {
                    houseRepairDoorVO.setNoMoney(DataUtils.doubleScale2(houseRepairDoorT.getRepairMoney() - (houseRepairDoorVO.getBalance() - houseRepairDoorVO.getBudgetMoney() * retained)));
                    houseRepairDoorVO.setPayBalance(DataUtils.doubleScale2(houseRepairDoorVO.getBalance() - houseRepairDoorVO.getBudgetMoney() * retained));
                    houseRepairDoorVO.setPayRate(0d);
                }

            }
        }
        houseRepairDoorVO.setPayMoney(houseRepairDoorT.getRepairMoney() - houseRepairDoorVO.getNoMoney());
        Double balance1 = DataUtils.subMoney(houseRepairDoorVO.getBalanceSum(), houseRepairDoorVO.getPayMoney());
        if (balance1 == 0) {
            houseRepairDoorVO.setBalanceRatio(0d);
            houseRepairDoorVO.setRepairRatio(0d);
        } else {
            Double balanceRatio = DataUtils.divideMoney(balance1, houseRepairDoorVO.getBudgetMoney()) * 100;
            houseRepairDoorVO.setBalanceRatio(balanceRatio);
            Double repairRatio = DataUtils.divideMoney(houseRepairDoorT.getRepairMoney(), houseRepairDoorVO.getBudgetMoney()) * 100;
            houseRepairDoorVO.setRepairRatio(repairRatio);
        }
        houseRepairCheckO.setHouseId(houseRepairDoorVO.getHouseId());
        houseRepairCheckO.setProjectId(houseRepairDoorVO.getProjectId());
        houseRepairCheckO.setRepairMoney(houseRepairDoorVO.getRepairMoney());
        houseRepairCheckO.setUpdateMoney(houseRepairDoorT.getRepairMoney());
        houseRepairDoorVO.setRepairMoney(houseRepairDoorT.getRepairMoney());
        houseRepairCheckO.setUpdater(getUsername());

        return toAjax(houseRepairDoorTService.updateHouseRepairDoorT(houseRepairDoorVO, houseRepairCheckO, houseProjectT));
    }

    @GetMapping(value = "/getCalculate")
    public AjaxResult getCalculateByProjectId(HouseProjectT houseProjectT) {
        HouseProjectT houseProjectVO = new HouseProjectT();
        HouseDoorT doorVO = new HouseDoorT();
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        Double trueMoney = 0d;
        Double spaceSum = 0d;
        String projectId = houseProjectT.getProjectId();
        Double price = houseProjectT.getPrice();
        houseProjectVO = houseProjectTService.selectHouseProjectTByProjectId(projectId);
        houseRepairDoorVO.setProjectId(projectId);
        List<HouseRepairDoorT> houseRepairDoorList = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorVO);
        for (HouseRepairDoorT vo : houseRepairDoorList) {
            if (vo.getSpace() == 0) {
                String houseid = vo.getHouseId();
                return AjaxResult.error(houseid + "该房屋编号面积为0,不能产生分摊");
            }
            spaceSum = DataUtils.sumMoney(spaceSum, vo.getSpace());
        }
        doorVO.setAreaName(houseProjectVO.getAreaName());
        doorVO = houseDoorTService.getBalanceMoneyAll(doorVO);
        if (price == null) {
            price = DataUtils.divideMoney(houseProjectVO.getProjectMoney(), spaceSum);
        }
//        BigDecimal price =(BigDecimal.valueOf(houseProjectVO.getProjectMoney())).divide(BigDecimal.valueOf(doorVO.getSpace()),6,BigDecimal.ROUND_HALF_UP);
        for (HouseRepairDoorT vo : houseRepairDoorList) {
            Double repairMoney = DataUtils.spaceMoney(vo.getSpace(), price);
            trueMoney = DataUtils.sumMoney(trueMoney, repairMoney);
        }
        houseProjectVO.setTrueMoney(trueMoney);
        houseProjectVO.setPrice(price);
        houseProjectVO.setSpace(spaceSum);
        return AjaxResult.success(houseProjectVO);
    }


    /**
     * 核算
     */
    @Log(title = "核算", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCalculate")
    public AjaxResult updateCalculate(@RequestBody HouseProjectT houseProjectT) {
        HouseProjectT projectVO = new HouseProjectT();
        List<HouseRepairDoorT> repairListTemp = new ArrayList<HouseRepairDoorT>();
        HouseRepairDoorT houseRepairDoorVO = new HouseRepairDoorT();
        houseRepairDoorVO.setProjectId(houseProjectT.getProjectId());
        //单价
        Double price = Double.valueOf(houseProjectT.getPrice());
        Double trueMoney = 0d;
        Double retained = getRetained();
        //查该工程编号下的房屋编号,维修类型自定义
        List<HouseRepairDoorT> houseRepairDoorList = houseRepairDoorTService.selectHouseRepairDoorTList(houseRepairDoorVO);
        //循环计算分摊金额
        for (HouseRepairDoorT vo : houseRepairDoorList) {
            HouseRepairDoorT temp = new HouseRepairDoorT();
            Double repaireMoney = 0d;
            repaireMoney = DataUtils.doubleScale2(vo.getSpace() * price);
            HouseSetupT houseSetupT = houseSetupTService.selectHouseSetupTByFeeStandards();
            //余额-应缴金额*剩余比率小于等于0 不足金额就是分摊金额
            if("Y".equals(houseSetupT.getBeInDebtSys())){
                if("Y".equals(houseSetupT.getBuckleRateSys())){
                    if (repaireMoney == 0) {
                        temp.setNoMoney(0d);
                        temp.setPayBalance(0d);
                        temp.setPayRate(0d);
                    } else if (vo.getBalance() <= vo.getBudgetMoney() * retained) {
                        if (vo.getBalanceSum() - vo.getBudgetMoney() * retained <= 0) {
                            temp.setNoMoney(0d);
                            temp.setPayBalance(repaireMoney);
                            temp.setPayRate(0d);
                        } else if (vo.getBalanceSum() - vo.getBudgetMoney() * retained >= repaireMoney) {
                            temp.setNoMoney(0d);
                            temp.setPayBalance(0d);
                            temp.setPayRate(repaireMoney);
                        } else {
                            temp.setNoMoney(0d);
                            temp.setPayRate(DataUtils.doubleScale2(vo.getBalanceSum() - vo.getBudgetMoney() * retained));
                            temp.setPayBalance(DataUtils.doubleScale2(repaireMoney - temp.getPayRate()));
                        }
                    } else if (vo.getBalance() - repaireMoney >= vo.getBudgetMoney() * retained) {
                        temp.setNoMoney(0d);
                        temp.setPayBalance(repaireMoney);
                        temp.setPayRate(0d);
                    } else {
                        if (vo.getBalanceSum() - vo.getBudgetMoney() * retained >= repaireMoney) {
                            temp.setNoMoney(0d);
                            temp.setPayBalance(DataUtils.doubleScale2(vo.getBalance()-vo.getBudgetMoney() * retained));
                            temp.setPayRate(DataUtils.doubleScale2(repaireMoney-temp.getPayBalance()));
                        } else {
                            temp.setNoMoney(0d);
                            temp.setPayRate(vo.getRateMoney());
                            temp.setPayBalance(DataUtils.doubleScale2(repaireMoney-temp.getPayRate()));
                        }
                    }
                }else{
                    temp.setNoMoney(0d);
                    temp.setPayBalance(repaireMoney);
                    temp.setPayRate(0d);
                }
            }else{
                if("Y".equals(houseSetupT.getBuckleRateSys())){
                    if (repaireMoney == 0) {
                        temp.setNoMoney(0d);
                        temp.setPayBalance(0d);
                        temp.setPayRate(0d);
                    } else if (vo.getBalance() <= vo.getBudgetMoney() * retained) {
                        if (vo.getBalanceSum() - vo.getBudgetMoney() * retained <= 0) {
                            temp.setNoMoney(repaireMoney);
                            temp.setPayBalance(0d);
                            temp.setPayRate(0d);
                        } else if (vo.getBalanceSum() - vo.getBudgetMoney() * retained >= repaireMoney) {
                            temp.setNoMoney(0d);
                            temp.setPayBalance(0d);
                            temp.setPayRate(repaireMoney);
                        } else {
                            temp.setNoMoney(DataUtils.doubleScale2(repaireMoney - (vo.getBalanceSum() - vo.getBudgetMoney() * retained)));
                            temp.setPayBalance(0d);
                            temp.setPayRate(DataUtils.doubleScale2(vo.getBalanceSum() - vo.getBudgetMoney() * retained));
                        }
                    } else if (vo.getBalance() - repaireMoney >= vo.getBudgetMoney() * retained) {
                        temp.setNoMoney(0d);
                        temp.setPayBalance(repaireMoney);
                        temp.setPayRate(0d);
                    } else {
                        if (vo.getBalanceSum() - vo.getBudgetMoney() * retained >= repaireMoney) {
                            temp.setNoMoney(0d);
                            temp.setPayBalance(DataUtils.doubleScale2(vo.getBalance()-vo.getBudgetMoney() * retained));
                            temp.setPayRate(DataUtils.doubleScale2(repaireMoney-temp.getPayBalance()));
                        } else {
                            temp.setNoMoney(DataUtils.doubleScale2(repaireMoney - (vo.getBalanceSum() - vo.getBudgetMoney() * retained)));
                            temp.setPayBalance(DataUtils.doubleScale2(vo.getBalance()-vo.getBudgetMoney() * retained));
                            temp.setPayRate(DataUtils.doubleScale2(repaireMoney-temp.getNoMoney()-temp.getPayBalance()));
                        }
                    }
                }else{

                    if (repaireMoney == 0) {
                        temp.setNoMoney(0d);
                        temp.setPayBalance(0d);
                        temp.setPayRate(0d);
                    } else if (vo.getBalance() <= vo.getBudgetMoney() * retained) {
                        temp.setNoMoney(repaireMoney);
                        temp.setPayBalance(0d);
                        temp.setPayRate(0d);
                    } else if (vo.getBalance() - repaireMoney >= vo.getBudgetMoney() * retained) {
                        temp.setNoMoney(0d);
                        temp.setPayBalance(repaireMoney);
                        temp.setPayRate(0d);
                    } else {
                        temp.setNoMoney(DataUtils.doubleScale2(repaireMoney - (vo.getBalance() - vo.getBudgetMoney() * retained)));
                        temp.setPayBalance(DataUtils.doubleScale2(vo.getBalance() - vo.getBudgetMoney() * retained));
                        temp.setPayRate(0d);
                    }

                }
            }
            temp.setProjectId(houseRepairDoorVO.getProjectId());
            temp.setHouseId(vo.getHouseId());
            temp.setRepairMoney(repaireMoney);
            temp.setPayMoney(repaireMoney - temp.getNoMoney());
            temp.setBalance(vo.getBalance());
            //当前余额➗应缴金额*100等于余额比率
            Double balance1 = DataUtils.subMoney(vo.getBalanceSum(), temp.getPayMoney());
            if (balance1 == 0) {
                temp.setBalanceRatio(0d);
                temp.setRepairRatio(0d);
            } else {
                Double balanceRatio = DataUtils.divideMoney(balance1, vo.getBudgetMoney()) * 100;
                temp.setBalanceRatio(balanceRatio);
                Double repairRatio = DataUtils.divideMoney(repaireMoney, vo.getBudgetMoney()) * 100;
                temp.setRepairRatio(repairRatio);
            }
            trueMoney = DataUtils.sumMoney(trueMoney, repaireMoney);
            repairListTemp.add(temp);
        }
        projectVO.setTrueMoney(Double.valueOf(trueMoney.toString()));
        projectVO.setPayMoney(Double.valueOf(trueMoney.toString()));
        projectVO.setProjectId(houseRepairDoorVO.getProjectId());
        houseRepairDoorTService.updateProjectAndRepair(projectVO, repairListTemp);

        return toAjax(1);
    }

    public Double getRetained() {
        HouseSetupT houseSetupT = new HouseSetupT();
        HouseSetupT list = houseSetupTService.selectHouseSetupTByFeeStandards();
        return list.getRetained();
    }


}
